package com.ktgj.customer.controller;

import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ktgj.common.core.constant.CustomerConstants;
import com.ktgj.common.log.annotation.TokenPermission;
import com.ktgj.customer.domain.MenuDTO;
import com.ktgj.customer.domain.dto.SysMenuLoginDTO;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ktgj.common.log.annotation.Log;
import com.ktgj.common.log.enums.BusinessType;
import com.ktgj.common.security.annotation.PreAuthorize;
import com.ktgj.customer.domain.Menu;
import com.ktgj.customer.service.IMenuService;
import com.ktgj.common.core.web.controller.BaseController;
import com.ktgj.common.core.web.domain.AjaxResult;
import com.ktgj.common.core.utils.poi.ExcelUtil;
import com.ktgj.common.core.web.page.TableDataInfo;

/**
 * 菜单Controller
 * 
 * @author ruoyi
 * @date 2021-10-15
 */
@ControllerAdvice
@RestController
@RequestMapping("/menu")
public class MenuController extends BaseController
{
    @Autowired
    private IMenuService menuService;

    /**
     * 查询菜单列表
     */
    @PreAuthorize(hasPermi = "system:menu:list")
    @GetMapping("/list")
    public TableDataInfo list(MenuDTO menu)
    {
        startPage();
        List<MenuDTO> list = menuService.selectMenuList(menu);
        return getDataTable(list);
    }

    /**
     * 导出菜单列表
     */
    @PreAuthorize(hasPermi = "system:menu:export")
    @Log(title = "菜单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MenuDTO menu) throws IOException
    {
        List<MenuDTO> list = menuService.selectMenuList(menu);
        ExcelUtil<MenuDTO> util = new ExcelUtil<MenuDTO>(MenuDTO.class);
        util.exportExcel(response, list, "菜单数据");
    }

    /**
     * 获取菜单详细信息
     */
    @PreAuthorize(hasPermi = "system:menu:query")
    @GetMapping(value = "/{menuId}")
    public AjaxResult getInfo(@PathVariable("menuId") Long menuId)
    {
        return AjaxResult.success(menuService.selectMenuById(menuId));
    }

    /**
     * 新增菜单
     */
    @PreAuthorize(hasPermi = "system:menu:add")
    @Log(title = "菜单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MenuDTO menu)
    {
        return toAjax(menuService.insertMenu(menu));
    }

    /**
     * 修改菜单
     */
    @PreAuthorize(hasPermi = "system:menu:edit")
    @Log(title = "菜单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MenuDTO menu)
    {
        return toAjax(menuService.updateMenu(menu));
    }

    /**
     * 删除菜单
     */
    @PreAuthorize(hasPermi = "system:menu:remove")
    @Log(title = "菜单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{menuIds}")
    public AjaxResult remove(@PathVariable Long[] menuIds)
    {
        return toAjax(menuService.deleteMenuByIds(menuIds));
    }

    /**
     * 导航菜单
     * @return
     */
    @RequestMapping("/nav")
    public AjaxResult nav(Long userId) {
        List<MenuDTO> menuList = menuService.getUserMenuList(userId);
        return AjaxResult.success(menuList);
    }

    @RequestMapping("/list/tree")
    public AjaxResult menuListTree() {
        List<MenuDTO> menuDTOS = menuService.menuListTree();
        return AjaxResult.success(menuDTOS);
    }

    @RequestMapping("/selectMenusByPositionId")
    public AjaxResult selectMenusByPositionId(@RequestBody Map<String, Object> map) {
        String model;
        if (map.get("model") != null && !map.get("model").equals("")){
            model = map.get("model").toString();
        }
        else {
            model = "customer";
        }
        Map<String, List<SysMenuLoginDTO>> positionId = menuService.selectMenusByPositionId(new Long(map.get("positionId").toString()), model);
        return AjaxResult.success(positionId);
    }

    @PostMapping("/queryMenuAll")
    public AjaxResult queryMenuAll() {
        return AjaxResult.success(menuService.queryMenuAll());
    }
}